//
//  AnimationSdudy.swift
//  NaiCha
//
//  Created by 唐安坤 on 2024/7/5.
//

import SwiftUI

struct AnimationSdudy: View {
    @State private var isPresented = false
    @State private var scale:Double = 1.0
    
    @State var circleColor:Color = .gray
    @State var starColor:Color = .white
    @State var angle:Double = 0.0
    var body: some View {
        
        VStack {
            ZStack{
                Circle()
                    .frame(width: 200, height: 200)
                    .foregroundStyle(circleColor)
                Image(systemName: "star.fill")
                    .font(Font.system(size: 100))
                    .foregroundStyle(starColor)
            }.onTapGesture {
                withAnimation(.easeInOut.repeatForever()) {
                    circleColor = .orange
                    starColor = .yellow
                    angle = 360
                }
            }
            
            HStack(spacing:20){
                ExtractedView(id: 1, scale: $scale)
                ExtractedView(id: 2, scale: $scale)
                ExtractedView(id: 3, scale: $scale)
                ExtractedView(id: 4, scale: $scale)
                ExtractedView(id: 5, scale: $scale)
            }.onAppear{
                scale = 0
            }
            
            Circle()
                .rotation(Angle(degrees: angle))
                .trim(from: 0,to: 0.5)
                .stroke(style: StrokeStyle(lineWidth: 10,lineCap: .round))
                .animation(.easeIn(duration: 2).repeatForever())
            
            
            Circle()
                .stroke(style: StrokeStyle(lineWidth: 20))
                .frame(width: 200, height: 200)
                .foregroundStyle(circleColor).overlay {
                    
                    Circle()
                        .rotation(Angle(degrees: angle))
                        .trim(from: 0,to: 0.1)
                        .stroke(style: StrokeStyle(lineWidth: 10,lineCap: .round))
                        .animation(.linear(duration: 1).repeatForever(autoreverses: false))
                }
            
        }
    }
}

#Preview {
    AnimationSdudy()
}

private struct ExtractedView: View {
    var id: Int
    @Binding var scale: Double;
    var body: some View {
        
        Circle()
            .foregroundStyle(.blue)
            .frame(width: 20,height: 20)
            .scaleEffect(scale)
            .animation(.easeIn(duration: 0.5).repeatForever().delay(0.1 * Double(id)), value: scale)
        
    }
}
